//
//  ProgressImageView.swift
//  新浪微博
//
//  Created by 焦林生 on 15/11/19.
//  Copyright © 2015年 焦林生. All rights reserved.
//

import UIKit

//带进度的图像视图
class ProgressImageView: UIImageView {

    //加载进度0~1
    var progress: CGFloat = 0 {
    
        didSet {
        
            progressView.progress = progress
        }
    }
    
    //MARK: 构造函数
    init() {
    
        super.init(frame: CGRectZero)
        
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()
        
        
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:懒加载控件
    private lazy var progressView: ProgressView = ProgressView()
    
}


//进度视图
private class ProgressView: UIView {

    //加载进度
    var progress: CGFloat = 0 {
    
        didSet {
            //下次刷新的时候调用
            setNeedsDisplay()
        }
    }
    
    private override func drawRect(rect: CGRect) {
        
        let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
        let r = min(rect.width, rect.height) * 0.5
        let start = CGFloat(-M_PI_2)
        let end = start + progress * 2 * CGFloat(M_PI)
        
        let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
        path.addLineToPoint(center)
        path.closePath()
        
        UIColor(white: 1.0, alpha: 0.5).setFill()
        
        path.fill()
    }
}



